<?php
/**
 * Created by PhpStorm.
 * User: skytop
 * Date: 16-8-29
 * Time: 下午11:01
 */

namespace Admin\Controller;

use Think\Controller;
use Admin\Model\Category;
class BlogController extends Controller
{
    //博文列表
    public function index(){
        $relationship=D('BlogRelation');
        $this->list=$res=$relationship->where("del!=1")->relation(true)->select();
        /*print_r($res);*/
        $this->display();
    }

    //回收站
    public function recycle()
    {
        //还原与删除公用判断
        if (isset($_GET['id'])) {
            $type=I('get.type')?$del=0:$del=1;
            $update = array(
                'id' => I('get.id'),
                'del' =>$del
            );
            $msg=$type?"删除":"还原";  //不知为何
            if (M('blog')->save($update)) {
                /*echo $type;*/
                $this->success("已{$msg}成功", $type?U('Blog/recycle'):U('Blog/index'));
            } else {
                $this->error("{$msg}失败", U('Blog/index'));
            }
        }else{
            $relationship = D('BlogRelation');
            $this->list = $res = $relationship->where("del=1")->relation(true)->select();
            $this->display('index');
        }
    }

    //添加博文
    public function blog(){
        if(IS_POST){
            print_r(I());
            $data=array(
                'title'=>I('post.title'),
                'content'=>I('post.content'),
                'time'=>time(),
                'click'=>I('post.click'),
                'cid'=>I('post.cid')
            );

            if(isset($_POST['aid'])){
                foreach($_POST['aid'] as $v){
                    $data['attr'][]=$v;
                }
            }
            /*print_r($data);*/

            $content=D("BlogRelation");

            if($content->create($data)){
                $content->relation(true)->add($data);
                echo "success";
            }else{
                $this->error($content->getError());
            }

        }else {
            $list = M('cate')->order('sort')->select();
            $this->list = Category::unlimitedForLevel($list);
            /*print_r($this->list);*/
            $this->attr = M('attr')->select();
            $this->display();
        }
    }

    //编辑器图片上传处理
    public function upload(){
        echo "sss";
    }
}